#include "widget.h"
#include "ui_widget.h"
#include<QPushButton>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建老师和学生对象
    this->zt=new Teacher(this);//传this放到对象树上
    this->st=new Student(this);

    //connect(zt,&Teacher::hungry,st,&Student::treat);//连接不带参数的信号和槽


    //连接带参数的信号和槽
    void (Teacher::*teachersingal)(QString)=&Teacher::hungry;//函数指针指向函数地址  信号
    void (Student::*studentdispose)(QString)=&Student::treat;//函数指针指向函数地址  槽函数
    connect(zt,teachersingal,st,studentdispose);
    //classIsover();//连接后再触发信号
    //点击下课的按钮再下课

    QPushButton* btn=new QPushButton("下课",this);
    this->resize(600,400);
    //connect(btn,&QPushButton::clicked,this,&Widget::classIsover);//信号和槽；四个参数分别是 信号发送者，发送的信号，信号接收者，处理的槽函数
    void (Teacher::*teachersingal2)(void)=&Teacher::hungry;//函数指针指向函数地址
    void (Student::*studentdispose2)(void)=&Student::treat;//函数指针指向函数地址
    connect(zt,teachersingal2,st,studentdispose2);
    connect(btn,&QPushButton::clicked,zt,teachersingal2);//信号连接信号

    //disconnect(btn,&QPushButton::clicked,zt,teachersingal2)//断开信号,参数和连接信号的参数一模一样
    //信号和槽函数参数要一一对应
}

Widget::~Widget()
{
    delete ui;
}

void Widget::classIsover()//调用下课，触发老师饿了的信号
{
    //emit触发信号的关键字
    //emit zt->hungry();
    emit zt->hungry("饺子");
}
